package com.harmony.pilot.completion.client;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 编译数据库客户端
 * 从Build Service获取编译命令信息
 */
@Slf4j
@Component
public class CompilationDatabaseClient {
    
    private final WebClient webClient;
    
    public CompilationDatabaseClient(@Value("${service.build.url}") String buildServiceUrl) {
        this.webClient = WebClient.builder()
                .baseUrl(buildServiceUrl)
                .build();
    }
    
    /**
     * 查询编译命令
     */
    public CompilationCommand queryCompilationCommand(String filePath, String projectRoot) {
        try {
            Map<String, Object> request = new HashMap<>();
            request.put("filePath", filePath);
            request.put("projectRoot", projectRoot);
            
            return webClient.post()
                    .uri("/build/compilation-database/query")
                    .bodyValue(request)
                    .retrieve()
                    .bodyToMono(CompilationCommand.class)
                    .block();
                    
        } catch (Exception e) {
            log.debug("Failed to query compilation command", e);
            return null;
        }
    }
    
    /**
     * 获取include路径
     */
    public List<String> getIncludePaths(String filePath, String projectRoot) {
        try {
            Map<String, Object> request = new HashMap<>();
            request.put("filePath", filePath);
            request.put("projectRoot", projectRoot);
            
            Map<String, Object> response = webClient.post()
                    .uri("/build/compilation-database/includes")
                    .bodyValue(request)
                    .retrieve()
                    .bodyToMono(Map.class)
                    .block();
            
            if (response != null && "200".equals(String.valueOf(response.get("code")))) {
                @SuppressWarnings("unchecked")
                List<String> includes = (List<String>) response.get("data");
                return includes != null ? includes : List.of();
            }
            
        } catch (Exception e) {
            log.debug("Failed to get include paths", e);
        }
        
        return List.of();
    }
    
    /**
     * 获取宏定义
     */
    public List<String> getDefines(String filePath, String projectRoot) {
        try {
            Map<String, Object> request = new HashMap<>();
            request.put("filePath", filePath);
            request.put("projectRoot", projectRoot);
            
            Map<String, Object> response = webClient.post()
                    .uri("/build/compilation-database/defines")
                    .bodyValue(request)
                    .retrieve()
                    .bodyToMono(Map.class)
                    .block();
            
            if (response != null && "200".equals(String.valueOf(response.get("code")))) {
                @SuppressWarnings("unchecked")
                List<String> defines = (List<String>) response.get("data");
                return defines != null ? defines : List.of();
            }
            
        } catch (Exception e) {
            log.debug("Failed to get defines", e);
        }
        
        return List.of();
    }
    
    /**
     * 编译命令信息
     */
    public static class CompilationCommand {
        private String file;
        private String directory;
        private String command;
        private List<String> includePaths;
        private List<String> defines;
        private CompilerOptions compilerOptions;
        
        // Getters and Setters
        public String getFile() { return file; }
        public void setFile(String file) { this.file = file; }
        
        public String getDirectory() { return directory; }
        public void setDirectory(String directory) { this.directory = directory; }
        
        public String getCommand() { return command; }
        public void setCommand(String command) { this.command = command; }
        
        public List<String> getIncludePaths() { return includePaths; }
        public void setIncludePaths(List<String> includePaths) { this.includePaths = includePaths; }
        
        public List<String> getDefines() { return defines; }
        public void setDefines(List<String> defines) { this.defines = defines; }
        
        public CompilerOptions getCompilerOptions() { return compilerOptions; }
        public void setCompilerOptions(CompilerOptions compilerOptions) { this.compilerOptions = compilerOptions; }
    }
    
    /**
     * 编译器选项
     */
    public static class CompilerOptions {
        private String std;
        private String target;
        private List<String> flags;
        
        // Getters and Setters
        public String getStd() { return std; }
        public void setStd(String std) { this.std = std; }
        
        public String getTarget() { return target; }
        public void setTarget(String target) { this.target = target; }
        
        public List<String> getFlags() { return flags; }
        public void setFlags(List<String> flags) { this.flags = flags; }
    }
}

